import api from "../../api/api"
import Toast from '@vant/weapp/toast/toast'
import Dialog from '@vant/weapp/dialog/dialog'
import { areaList } from '@vant/area-data'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressDetailData: {},
    name: '',
    tel: '',
    addressDetail: '',
    areaName: '',
    areaCode: '',
    postalCode: '',
    show: false,
    areaList,
    isDefault: false,
    province: '',
    city: '',
    county: '',
    aid: null,
  },
  async getAddressDetailByAid(aid) {
    let tokenString = wx.getStorageSync('token')
    const res = await api.findAddressByAid({ aid, tokenString })
    if (res.data.code == 40000) {
      console.log('地址回显', res)
      this.setData({
        province: res.data.result[0].province,
        city: res.data.result[0].city,
        county: res.data.result[0].county
      })
      this.setData({
        areaName: res.data.result[0].province + ' ' + res.data.result[0].city + ' ' + res.data.result[0].county,
        areaCode: res.data.result[0].areaCode,
        name: res.data.result[0].name,
        tel: res.data.result[0].tel,
        province: res.data.result[0].province,
        city: res.data.result[0].city,
        county: res.data.result[0].county,
        addressDetail: res.data.result[0].addressDetail,
        postalCode: res.data.result[0].postalCode,
        isDefault: res.data.result[0].isDefault.toString()
      })
    }
  },
  showArea() {
    this.setData({
      show: true
    })
  },
  hideArea() {
    this.setData({
      show: false
    })
  },
  onConfirm(val) {
    let names = []
    val.detail.values.forEach((v) => {
      names.push(v.name)
    })
    this.setData({
      province: names[0],
      city: names[1],
      county: names[2]
    })
    names = names.join(' ')
    this.setData({
      areaName: names,
      areaCode: val.detail.values[2].code,
      show: false
    })
  },
  defaultChange(e) {
    this.setData({
      isDefault: e.detail
    })
  },
  async edit(e) {
    let aid = this.data.aid
    let province = this.data.province
    let city = this.data.city
    let county = this.data.county
    let tokenString = wx.getStorageSync('token')
    let name = e.detail.value.name
    let tel = e.detail.value.tel
    let addressDetail = e.detail.value.addressDetail
    let postalCode = e.detail.value.postalCode
    let isDefault = this.data.isDefault ? 1 : 0
    const res = await api.editAddress({ tokenString, province, city, county, name, tel, addressDetail, postalCode, isDefault, aid })
    if (res.data.code == 30000) {
      console.log('编辑地址', res)
      Toast.success(res.data.msg)
      setTimeout(() => {
        wx.navigateBack()
      }, 1500)
    }
  },
  delete() {
    let aid = this.data.aid
    let tokenString = wx.getStorageSync('token')
    Dialog.confirm({
      message: '确定要删除该地址信息吗？',
    }).then(async () => {
      const res = await api.deleteAddress({ aid, tokenString })
      if (res.data.code == 10000) {
        console.log('删除地址', res);
        Toast.success(res.data.msg)
        setTimeout(() => {
          wx.navigateBack()
        }, 1500)
      }
    }).catch(() => {
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let aid = options.aid
    this.setData({
      aid
    })
    this.getAddressDetailByAid(aid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})